/**
    Copyright (C) 2001, 2002, 2003  Michael Ahlberg, Måns Rullgård

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
**/

#ifndef _TCLIST_H
#define _TCLIST_H

#ifdef __cplusplus
extern "C" {
#endif

#define TC_LOCK_NONE   0
#define TC_LOCK_SLOPPY 1
#define TC_LOCK_STRICT 2

    typedef int (*tc_list_cmp_fn) (const void *, const void *);
    typedef void (*tc_list_free_fn) (void *);

    typedef struct tc_list_item tc_list_item_t;
    typedef struct tc_list tc_list_t;


    /* Create new empty tc_list_t */
    extern tc_list_t *tc_list_new(int locking);

    /* Free resources used by list. */
    extern int tc_list_free(tc_list_t *);

    /* Destroy list calling lfree with data in each item. */
    extern int tc_list_destroy(tc_list_t *lst, tc_list_free_fn lfree);

    /* Remove item from tc_list_t */
    extern void tc_list_remove(tc_list_t *lst, tc_list_item_t *l, tc_list_free_fn fr);

    /* Add to end of tc_list_t */
    extern int tc_list_push(tc_list_t *lst, void *p);

    /* Add to start of tc_list_t */
    extern int tc_list_unshift(tc_list_t *lst, void *p);

    /* Remove and return first element in tc_list_t */
    extern void *tc_list_shift(tc_list_t *lst);

    /* Remove and return last element in tc_list_t */
    extern void *tc_list_pop(tc_list_t *lst);

    /* Find element in list equal to p as determined by comparison
     * function cmp. Return 0 if found. */
    extern int tc_list_find(tc_list_t *lst, void *p, void *ret, tc_list_cmp_fn cmp);

    /* Find element in list equal to p as determined by comparison
     * function cmp. Add to end of list if not found. */
    extern int tc_list_search(tc_list_t *lst, void *p, void *ret, tc_list_cmp_fn cmp);

    /* Remove element matching p from list. Return deleted element. */
    extern int tc_list_delete(tc_list_t *lst, void *p, tc_list_cmp_fn cmp, tc_list_free_fn);

    /* Remove all elements matching p from list. Return # of elements removed. */
    extern int tc_list_delete_matched(tc_list_t *lst, void *p, tc_list_cmp_fn cmp,
                                      tc_list_free_fn);

    extern void *tc_list_next(tc_list_t *lst, tc_list_item_t **l);
    extern void *tc_list_next_matched(tc_list_t *lst, tc_list_item_t **l,
                                      void *key, tc_list_cmp_fn cmp);

    extern void *tc_list_prev(tc_list_t *lst, tc_list_item_t **l);
    extern void *tc_list_prev_matched(tc_list_t *lst, tc_list_item_t **l,
                                      void *key, tc_list_cmp_fn cmp);

    /* Unlock list item */
    extern int tc_list_unlock(tc_list_t *lst, tc_list_item_t *l);

    /* Returns item count. */
    extern unsigned long tc_list_items(tc_list_t *lst);

    extern int tc_list_isfirst(tc_list_t *lst, tc_list_item_t *li);

    extern int tc_list_islast(tc_list_t *lst, tc_list_item_t *li);

    extern void *tc_list_head(tc_list_t *lst);
    extern void *tc_list_tail(tc_list_t *lst);

#ifdef __cplusplus
}
#endif

#endif
